<?php
require_once("../includes/config.inc.php");
require("authentication-check.inc.php");
require_once("../includes/FileDataAccess.inc.php");

$pageTitle = "Blog Details";
$pageDescription = "";

//Set defaults
$file = array();
$file['fileId'] = 0;
$file['fileName'] = "";
$file['fileDescription'] = "";
$file['fileExtension'] = "";
$file['fileSize'] = 0;

// Set up the $fda object 
$fda = new FileDataAccess(getDBLink());

// Create an empty array to store input validation errors
// (we'll use this array when we get to the validation code)
$validationErrors = array();
// we'll allow these types of files to be uploaded
$allowed_file_types = array('image/pjpeg','image/jpeg','image/JPG','image/X-PNG','image/PNG','image/png','image/x-png');
// the images must be less than 1MB (1000000 bytes)
$max_file_size = 1000000;

if($_SERVER['REQUEST_METHOD'] == "GET"){
	
	if(isset($_GET['fileId'])){
		$file = $fda->getFileById($_GET['fileId']); 
	}

}elseif($_SERVER['REQUEST_METHOD'] == "POST"){
	
	// Get the user input and stuff it into the $page array
	$file['fileId'] = $_POST['fileId']; // hidden input
	$file['fileName'] = $_POST['fileName']; // hidden input
	$file['fileSize'] = $_POST['fileSize']; // hidden input
	$file['fileExtension'] = $_POST['fileExtension']; // hidden input
	$file['fileDescription'] = $_POST['fileDescription']; 
	
	// Validate the input and get validation errors (if there are any)
	$validationErrors = validateFileInput($file, $allowed_file_types, $max_file_size);

	if(empty($validationErrors)){

		// STEP 1 - process the file upload
		

		// Insert or Update (depends on $file['fileId'])
		if($file['fileId'] > 0){
			// UPDATE
			$file = $fda->updateFile($file);
		}else{
			// INSERT
			$file = $fda->insertFile($file);
		}

		// STEP 2 - WHAT HAPPENS IF YOU UPLOAD FILES WITH THE SAME NAMES? 
	    		
			
		header("Location: " . PROJECT_DIR . "control-panel/file-list.php");
		exit();
	}
	

}else{
	// we only accept GET and POST requests
	header("Location: " . PROJECT_DIR . "error.php");
	exit();
}

require("../includes/header.inc.php");
?>
<main>
	<div class="content-frame">
		<h3>File Details</h3>
		
		<!--DON'T FORGET TO SET THE enctype ATTRIBUTE ON THE FORM TAG!!!!-->
		<form class="control-panel" method="POST" enctype="multipart/form-data" action="<?php echo($_SERVER['PHP_SELF']) ?>">
			
			<input type="hidden" name="fileId" value="<?php echo($file['fileId']); ?>" />
			<input type="hidden" name="fileName" value="<?php echo($file['fileName']); ?>" />
			<input type="hidden" name="fileExtension" value="<?php echo($file['fileExtension']); ?>" />
			<input type="hidden" name="fileSize" value="<?php echo($file['fileSize']); ?>" />
			
			<?php
			// STEP 3 - display the image (if there is one to display)
			
			?>

			<label>
			Select a file
			<?php
			// STEP 4 - display error messages related to the file upload (if there are any to display)
			?>
			</label>
			<input type="file" name="upload" />
						
			<label>
				Description
				<?php echo(isset($validationErrors['fileDescription']) ? wrapValidationMsg($validationErrors['fileDescription']) : ""); ?>
			</label>

			<textarea name="fileDescription"><?php echo($file['fileDescription']); ?></textarea>
						
			<br>	
			<input type="submit" value="SAVE" />

		</form>
		
	</div>
</main>
		
<?php
if(!empty($sideBar)){
	require("../includes/" . $sideBar);
}

require("../includes/footer.inc.php");

//////////////////////
// Functions
/////////////////////

function validateFileInput($file, $allowed_file_types, $max_file_size){

	// we'll populate this array with any errors that we discover.
	$errors = array();

	if(empty($file['fileDescription'])){
		$errors['fileDescription'] = "You must enter a description";
	}

	// STEP 5 - Finish validation code
	

	return $errors;
}

?>

